package cc.telecomdigital.tdstock.trading.widget;

import cc.telecomdigital.tdstock.R;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AppDialogOKCancel extends Dialog implements
	Button.OnClickListener, OnCancelListener
{
	public interface IAppDialogOKCancel
	{
		/** What to do when an OK is pressed **/		
		void OnOK_Click(DialogInterface iDialog);

		/** What to do when Canel is pressed **/		
		void OnCancel_Click(DialogInterface iDialog);

		/** What to do when user pressed back button **/		
		void OnDialogCancel(DialogInterface iDialog);
	}

	protected IAppDialogOKCancel	interfaceHandle;

	/** Place in the owner for Dialog box **/	
	public AppDialogOKCancel(Context iContext)
	{
		super(iContext, R.style.TradeDialog_Style);
		setContentView(R.layout.app_dialog_okcancel);
		SetClickableObject();
	}

	/** Place in the owner for Dialog box **/	
	public AppDialogOKCancel(Context iContext, int iLayout)
	{
		super(iContext, R.style.TradeDialog_Style);
		setContentView(iLayout);
		SetClickableObject();
	}	
	
	protected void SetClickableObject()
	{
		Button buttonObj;

		buttonObj = (Button) findViewById(R.id.app_dialog_okcancel_OKButton);
		buttonObj.setOnClickListener(this);

		buttonObj = (Button) findViewById(R.id.app_dialog_okcancel_CancelButton);
		buttonObj.setOnClickListener(this);
		this.setOnCancelListener(this);		
	}
	
	/** Set the Button Text from resource id; OK, Cancel are the default **/	
	public void SetButtonText(int iOKResID, int iCancelResID)
	{
		((TextView) findViewById(R.id.app_dialog_okcancel_OKButton))
				.setText(iOKResID);
		((TextView) findViewById(R.id.app_dialog_okcancel_CancelButton))
				.setText(iCancelResID);
	}

	/** Set the Button Text; OK, Cancel are the default **/	
	public void SetButtonText(String iOKText, String iCancelText)
	{
		((TextView) findViewById(R.id.app_dialog_okcancel_OKButton))
				.setText(iOKText);
		((TextView) findViewById(R.id.app_dialog_okcancel_CancelButton))
				.setText(iCancelText);
	}

	/** Set the Dialog Content **/
	public void SetTextContent(String iContent)
	{
		TextView textViewObj;

		textViewObj = (TextView) findViewById(R.id.app_dialog_okcancel_Content);
		iContent = iContent.replace("\\n", "\n");		
		textViewObj.setText(iContent);
	}

	/** Show the dialog box with the return interface, the dialog is dimissed when
	    either of one action is done **/
	public void show(IAppDialogOKCancel iInterface, boolean iCancelable)
	{
		interfaceHandle = iInterface;
		this.setCancelable(iCancelable);
		this.show();
	}

	@Override
	public void onClick(View iViewObj)
	{
		switch (iViewObj.getId())
		{
			case R.id.app_dialog_okcancel_OKButton:
				this.dismiss();
				if (interfaceHandle != null)
					interfaceHandle.OnOK_Click(this);
				break;

			case R.id.app_dialog_okcancel_CancelButton:
				this.dismiss();
				if (interfaceHandle != null)
					interfaceHandle.OnCancel_Click(this);
				break;
		}
	}

	@Override
	public void onCancel(DialogInterface iDialog)
	{
		// TODO Auto-generated method stub
		iDialog.dismiss();
		if (interfaceHandle != null)
			interfaceHandle.OnDialogCancel(this);
	}
}
